package com.xiaojie.test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * JwtTest001
 * Administrator
 * 2021/4/27
 * 23:10
 **/
public class JwtTest001 {

    private static String signKey="xiaojie";
    public static void main(String[] args) {
        /**
         * 加密
         */
        long now = System.currentTimeMillis();
        //过期时间为一分钟
        long exp=now+1000*60;
        JwtBuilder jwtBuilder = Jwts.builder()
                .claim("userImg", "123456")
                .signWith(SignatureAlgorithm.HS256, signKey)
                .setExpiration(new Date(exp));
        /**
         * 解密
         */
        System.out.println(jwtBuilder.compact());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Claims body = Jwts.parser().setSigningKey(signKey).
                parseClaimsJws(jwtBuilder.compact()).getBody();
        System.out.println(body);
    }

}
